// Definition

#include "Bullet.h"

Bullet::Bullet() : mActive(false)
{
	
}

Bullet::~Bullet()
{
	
}

void Bullet::Load() // The Load function under the Bullet class
{
	mSprite.Load("bullet1.png");
}

void Bullet::Unload()
{
	mSprite.Unload();
}

void Bullet::Update(float deltaTime)
{
	if (mActive)
	{
		mPosition += mVelocity * deltaTime;

		const int kWIN_WIDTH = IniFile_GetInt("WinWidth", 800); // 800 is default value, it will be used if cant find win width
		const int kWIN_HEIGHT = IniFile_GetInt("WinHeight", 600);
		const int kTHRESHOLD = mSprite.GetWidth();
		if (mPosition.x < -kTHRESHOLD || mPosition.x > kWIN_WIDTH + kTHRESHOLD ||
			mPosition.y < -kTHRESHOLD || mPosition.y > kWIN_HEIGHT + kTHRESHOLD)
		{
			Kill();
		}
	}
}

void Bullet::Render()
{
	if (mActive)
	{
		int width = mSprite.GetWidth();
		int heigth = mSprite.GetHeight();
		SVector2 offset(width * 0.5f, heigth * 0.5f);
		SVector2 renderPos = mPosition - offset;
		mSprite.SetPosition(renderPos);
		mSprite.Render();
	}
}

void Bullet::Fire(const SVector2& pos, const SVector2& vel)
{
	mPosition = pos;
	mVelocity = vel;
	mActive = true;
}

void Bullet::Kill()
{
	mActive = false;
}

bool Bullet::IsActive()
{
	return this->mActive;
}